package com.algorithms.leetcode.LinkList;

import com.algorithms.leetcode.ListNode;

/**
 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。

 * <br>
 */
public class leetcode_206_reverseList {

    /**
     * 迭代法
     * @param head
     * @return
     */
    public ListNode reverseListV1(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;
        while (curr != null){
            ListNode tmp = curr.next;
            //反转
            curr.next = prev;
            //往下移动
            prev = curr;
            curr = tmp;
        }
        return prev;
    }

    /**
     * 迭代法
     * @param head
     * @return
     */
    public ListNode reverseListV2(ListNode head) {
        //终止条件
       if(head == null || head.next == null){
           return head;
       }
       //递归 传入子串
       ListNode p = reverseListV2(head.next);
       head.next.next = head;
       head.next = null;
       return p;
    }
}
